package com.ruoyi.config.pdf.method;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.ruoyi.config.pdf.common.Template;
import com.ruoyi.config.pdf.common.TemplateEnum;
import com.ruoyi.config.pdf.utils.PdfUtils;
import com.ruoyi.config.pdf.vo.PdfReqVo;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yjc
 * @create 2023-04-20 16:50
 */
@Slf4j
//@RestController
public class PdfCreate {

    //    @PostMapping("/create/pdf")
    public static String createPdf(PdfReqVo reqVo) throws Exception {
        //选择模板
        log.info("reqVo:{}", reqVo);
        Template template = new Template();
        TemplateEnum templateEnum = TemplateEnum.getTemplateEnum(reqVo.getTemplate());
        switch (templateEnum) {
            case TEMPLATE1:
                template.template1();
                break;
            case TEMPLATE2:
                template.template2();
                break;
            case TEMPLATE3:
                template.template3();
                break;
            case TEMPLATE4:
                template.template4();
                break;
            case TEMPLATE5:
                template.template5();
                break;
            case TEMPLATE6:
                template.template6();
                break;
            case TEMPLATE7:
                template.template7();
                break;
            case TEMPLATE8:
                template.template8();
                break;
        }

        // TODO 上传服务器
        // 1、文件保存路径
//        String dest = "C:\\wusihao\\pdfTest" + ".pdf";
        String dest = "D:\\JAVA\\javaCode\\ThreeProject\\admin\\src\\main\\java\\com\\ruoyi\\pdfTest" + ".pdf";
        // 2、创建PdfDocument实例对象
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdfDoc = new PdfDocument(writer);
        //3、创建空页
        PdfPage pdfPage = pdfDoc.addNewPage();
        //4.1、创建一个Document实例对象
        Document document = new Document(pdfDoc);

        //5、自定义内容
        //数据库获取内容
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            list.add("内容" + i);
        }
        System.out.println(list);


        Table table = PdfUtils.getTable(reqVo, template);

        //加入表格
        document.add(table);


        //6、关闭document
        document.close();
        System.out.println("PDF Created");

        Map<String, Image> sealImgMap = reqVo.getSealMap();
        Map<String, Image> signImgMap = reqVo.getSignMap();
//        Map<String, Image> sealImgMap = new HashMap<>();
//        Map<String, Image> signImgMap = new HashMap<>();

        //获取不到图片时，刷新下项目
//        sealImgMap.put("经办人", new Image(ImageDataFactory.create(CreatePdf.class.getClassLoader().getResource("static/picture/seal1.png"))));
//        sealImgMap.put("负责人", new Image(ImageDataFactory.create(CreatePdf.class.getClassLoader().getResource("static/picture/seal1.png"))));
//        sealImgMap.put("申报单位", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/seal1.png"))));
//        sealImgMap.put("考试机构：", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/seal1.png"))));
//
//
//        signImgMap.put("审核人", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/sign1.png"))));
//        signImgMap.put("审批人", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/sign1.png"))));
//        signImgMap.put("单位经办人", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/sign1.png"))));
//        signImgMap.put("单位负责人", new Image(ImageDataFactory.create(PdfCreate.class.getClassLoader().getResource("static/picture/sign1.png"))));


        String url = PdfUtils.setSignAndSealRect(dest, sealImgMap, signImgMap, 1, template);

        return url;
    }
}
